package antrace;

public class HighscoreOverlay extends Overlay {
	
	private String[] highscores;
	
	public HighscoreOverlay() {
		super(AntRace.midlet.loadImage("/images/highscoreBG.png"));
		texts = new OverlayText[5];
		
//		texts[0] = new OverlayText("Lisa", 25, 200);
//		texts[1] = new OverlayText("Ante", 25, 220);
//		texts[2] = new OverlayText("Tennis", 25, 240);
//		texts[3] = new OverlayText("Jonny", 25, 260);
//		texts[4] = new OverlayText("Danny", 25, 280);
//		
//		texts[5] = new OverlayText("10586", 120, 200);
//		texts[6] = new OverlayText("9865", 120, 220);
//		texts[7] = new OverlayText("4035", 120, 240);
//		texts[8] = new OverlayText("4001", 120, 260);
//		texts[9] = new OverlayText("189", 120, 280);
		update();
		//font = AntRace.midlet.loadFont("/images/fonts/Acknowledge30.mvlw");
	}
	
	public void update() {
		
		highscores = AntRace.midlet.loadStrings(AntRace.FILENAME_HIGHSCORE);
		highscores = sortHighscore(highscores);
		int j = 1;
		for (int i = 4; i >= 0; i--) {
			
			texts[i] = new OverlayText(highscores[j], 55, 160+30*i);
			j++;
			//System.out.println(highscores[i]);
		}
		
	}

	
	public static String[] sortHighscore(String[] oldHighscore) {
		
		String[] names = new String[oldHighscore.length];
		int[] points = new int[oldHighscore.length];
		
		for (int i = 0; i < oldHighscore.length; i++) {
			names[i] = oldHighscore[i].substring(0, 6);
			points[i] = Integer.parseInt( oldHighscore[i].substring(6, oldHighscore[i].length()) );	
		}
		
		int tmp, size = points.length-1;
		String tmpStr = "";
        for (int i = 0; i <= size; i++) {
			for (int j = size; j > i; j--) {
				if (points[j] < points[j-1]) {
					tmp = points[j-1];
					tmpStr = names[j-1];
					points[j-1] = points[j];
					names[j-1] = names[j];
					points[j] = tmp;
					names[j] = tmpStr;
				}
			}
		}
		
		String[] highscore = new String[names.length];
		for (int i = 0; i < names.length; i++) {
			highscore[i] = names[i] + points[i];
			//System.out.println(test[i]);
		}
		
		return highscore;
	}
	
	public void draw(){
		super.draw();
		AntRace.midlet.textFont(AntRace.selectedFont);
		AntRace.midlet.text("Press any key", 25, 300);
		AntRace.midlet.textFont(AntRace.font);
	}
	
	public void keyPressed(int keyCode){
		this.toggleVisible();
	}
	
}
